package com.app.qbake.module.square;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.wind.base.adapter.BaseDelegateRecyclerAdapter;
import com.wind.base.mvp.view.BasePageFragment;
import com.wind.base.request.BaseRequest;
import com.wind.base.utils.DateUtil;
import com.wind.coder.annotations.Api;
import com.wind.coder.annotations.Heros;
import com.wind.coder.annotations.Param;
import com.wind.data.square.SquareRequest;
import com.wind.data.square.SquareResponse;
import com.wind.data.square.bean.BannerItem;
import com.wind.data.square.bean.BannerList;
import com.app.qbake.R;
import com.app.qbake.module.square.adapter.SearchGoodsAdapter;
import com.app.qbake.module.square.adapter.SquareAdapter;
import com.app.qbake.module.square.di.SquareComponent;
import com.app.qbake.module.square.event.BannerLoadedEvent;
import com.app.qbake.module.square.mvp.SquareView;
import com.app.qbake.module.square.presenter.SquarePresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by wind on 2018/9/4.
 */
@Heros(
        param = @Param(
                viewCanonicalName = "com.app.qbake.module.square.mvp.SquareView",
                responseCanonicalName = "com.wind.data.square.SquareResponse",
                requestCanonicalName = "com.wind.data.square.SquareRequest",
                page = true
        ),
        api = @Api(httpMethod = Api.HttpMethod.GET, url = "home/getShop")
)
public class SquareFragment extends BasePageFragment<SquareView, SquarePresenter, SquareComponent, SquareResponse>
        implements SquareView {
    private static final String ARGS_KEY_KEYWORDS = "args_key_keywords";
    private String mType;
    private String mKeywords;
    public static final String ARGS_KEY_TYPE="args_key_type";
    @Inject
    SquarePresenter presenter;

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_square;
    }

    @Override
    public SquarePresenter createPresenter() {
        return presenter;
    }

    @Override
    protected BaseDelegateRecyclerAdapter getAdapter() {
        if (TextUtils.isEmpty(mKeywords)){
            return new SquareAdapter(getActivity());
        }else {
            return new SearchGoodsAdapter(getActivity());
        }

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mType=getArguments().getString(ARGS_KEY_TYPE);
        mKeywords=getArguments().getString(ARGS_KEY_KEYWORDS);
        super.onViewCreated(view, savedInstanceState);
        //mPrv.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        String time= DateUtil.get(System.currentTimeMillis(),"HH:mm");
        mPrv.getLoadingLayoutProxy().setLastUpdatedLabel("最后更新："+time);
    }

    @Override
    protected BaseRequest buildRequest(boolean isFirstPage) {
        SquareRequest request=new SquareRequest();
        request.setFirstPage(isFirstPage);
        request.setRequestTime(System.currentTimeMillis()+"");
        request.setType(mType);
        request.setCategoryId(mKeywords);
        return request;
    }

    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        return null;
    }

    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        if (TextUtils.isEmpty(mKeywords)){
            return super.getLayoutManager();
        }else {
            GridLayoutManager manager=new GridLayoutManager(getActivity(),2);
            return manager;
        }

    }

    public static SquareFragment getInstance(String type, String keywords) {
        SquareFragment f=new SquareFragment();
        Bundle args=new Bundle();
        args.putString(ARGS_KEY_TYPE,type);
        args.putString(ARGS_KEY_KEYWORDS,keywords);
        f.setArguments(args);
        return f;
    }

    @Override
    public void showPage(SquareResponse response) {
        super.showPage(response);

        List<BannerItem> bannerItemList=response.getData().getBanner();
        EventBus.getDefault().post(new BannerLoadedEvent(new BannerList(bannerItemList)));

    }

    public void disableRecyclerView(){
        mPrv.setMode(PullToRefreshBase.Mode.DISABLED);
        mPrv.getRefreshableView().setNestedScrollingEnabled(false);
    }

    /*public static Fragment getInstance(String keywords){
        SquareFragment f=new SquareFragment();
        Bundle args=new Bundle();
        args.putString(ARG_KEY_KEYWORDS,keywords);
        f.setArguments(args);
        return f;
    }*/
}
